package m.jclecai.com.adapter.independentLotteryAdapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.MissDataBean;

/**
 * Created by andy on 2017/6/27.
 */

public class PLS_SD_QXCA_LotteryAdapter extends BaseAdapter {

    private final Context context;
    private List<MissDataBean.Codes> codes;

    public PLS_SD_QXCA_LotteryAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<MissDataBean.Codes> codes) {
        this.codes = codes;
    }


    @Override
    public int getCount() {
        return codes == null ? 0 : codes.size();
    }

    @Override
    public Object getItem(int position) {
        return codes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=convertView;
        ViewHolder viewHolder=null;
        if (view == null) {
            view=View.inflate(context, R.layout.item_list_pls_sd_qxca,null);
            viewHolder=new ViewHolder();
            viewHolder.mTvTerm= ((TextView) view.findViewById(R.id.tv_term));
            viewHolder.mTvNumber= ((TextView) view.findViewById(R.id.tv_number));
            view.setTag(viewHolder);
        }else{
            viewHolder= ((ViewHolder) view.getTag());
        }
        MissDataBean.Codes codes = this.codes.get(position);
        String issue =codes.getIssue();

        String[] issues = codes.getCode().split("\\|");             //如果有两种颜色的话，服务器是会通过这个来返回的；

        String redNumber="";
        String blueNumber="";

        if(issues.length>0){
            //这里我们要做处理；
            String[] split = issues[0].split(",");

            for (String s : split) {
                redNumber+="<font color='#fb4e46'>"+s+"</font>&nbsp&nbsp";
            }

            if(issues.length>1){
                //这里表示有蓝球；
                split=issues[1].split(",");

                for (String s : split) {
                   blueNumber+="<font color='#3097fd'>"+s+"</font>&nbsp&nbsp";
                }
            }

        }

        if (issue.length()>7) {
            //2017062834，这里表示按天算；
            viewHolder.mTvTerm.setText(issue.substring(issue.length()-2)+"期");
        }else{
            //2017170,这里表示按年算；
            viewHolder.mTvTerm.setText(issue.substring(issue.length()-3)+"期");
        }

        viewHolder.mTvNumber.setText(Html.fromHtml(redNumber+blueNumber));
        return view;
    }

    private class ViewHolder {

        TextView mTvTerm;
        TextView mTvNumber;
    }
}
